﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using COMOSOFT.Class;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace COMOSOFT.JobManagement.USC
{
    public partial class UscActionLog : UserControl
    {
        int JFUID,Handler;
        string fn = null;
        Dataset.DataSet_JobManagementTableAdapters.sp_JobFollupTableAdapter adt = new Dataset.DataSet_JobManagementTableAdapters.sp_JobFollupTableAdapter();
        List<string> ListValue = new List<string>();
        List<string> ListAttachFile = new List<string>();
        public UscActionLog( int ID,int _Handler)
        {
            InitializeComponent();
            this.JFUID = ID;
            this.Handler = _Handler;
        }
        public void LoadData() 
        {
            try
            {                
                if (JFUID >= 0)
                {
                    if (Handler == ClassShare.Usid)
                    {
                        layout_RequestTime.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                        layout_Status.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                        layout_Comment.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                    }
                    ListAttachFile.Clear();
                    txt_actionContent.Properties.ReadOnly = true;
                    bnt_FileAttach.Properties.Buttons[0].Enabled = false;                                      
                    sp_JobFollupTableAdapter.Fill(dataSet_JobManagement.sp_JobFollup, JFUID);
                    txt_actionRequestTime.Properties.ReadOnly = true;
                    txt_Status.Properties.ReadOnly = true;
                    txt_Remark.Properties.ReadOnly = true;
                    DataTable dtb = adt.GetData(JFUID);
                    if (dtb.Rows.Count > 0)
                    {
                        string[] FAT = dtb.Rows[0]["FileAttach"].ToString().Split(new string[] { "," }, 0);
                        for (int i = 0; i < FAT.Count(); i++)
                        {
                            if (FAT[i].ToString() != "")
                            {
                                ListAttachFile.Add(FAT[i].ToString());
                            }
                        }
                        if(ListAttachFile.Count>0)
                           DisplayAtachfiles(ListAttachFile, bnt_FileAttach);
                    }
                }
                else
                {
                    sp_JobFollupTableAdapter.Fill(dataSet_JobManagement.sp_JobFollup, -1);
                    txt_actionContent.Focus();
                    layout_RequestTime.Visibility = DevExpress.XtraLayout.Utils.LayoutVisibility.Always;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void DisplayAtachfiles(List<string> List, ButtonEdit bnt)
        {
            try
            {
                while (bnt.Properties.Buttons.Count > 1)
                {
                    bnt.Properties.Buttons.RemoveAt(0);
                }
                foreach (string fn in List)
                {
                    FileInfo finfo = new FileInfo(ClassShare.JobAsign + @"\\" + fn);
                    Image img = null;
                    if (finfo.Extension.ToLower() == ".pdf")
                    {
                        img = imageCollection1.Images[0];
                    }
                    else if (finfo.Extension.ToLower() == ".xls")
                    {
                        img = imageCollection1.Images[1];
                    }
                    else if (finfo.Extension.ToLower() == ".xlsx")
                    {
                        img = imageCollection1.Images[1];
                    }
                    else if (finfo.Extension.ToLower() == ".doc")
                    {
                        img = imageCollection1.Images[2];
                    }
                    else if (finfo.Extension.ToLower() == ".docx")
                    {
                        img = imageCollection1.Images[2];
                    }
                    else
                        img = imageCollection1.Images[3];

                    DevExpress.Utils.SerializableAppearanceObject serializableAppreanceObje = new DevExpress.Utils.SerializableAppearanceObject();
                    serializableAppreanceObje.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Underline);
                    serializableAppreanceObje.ForeColor = System.Drawing.Color.Blue;
                    serializableAppreanceObje.Options.UseFont = true;
                    serializableAppreanceObje.Options.UseForeColor = true;
                    DevExpress.XtraEditors.Controls.EditorButton b = new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, finfo.Name.Substring(0,finfo.Name.Length-17), -1, true, true, false, DevExpress.XtraEditors.ImageLocation.MiddleLeft, img, new DevExpress.Utils.KeyShortcut(System.Windows.Forms.Keys.None), serializableAppreanceObje, "", null, null, true);
                    if (JFUID < 0)
                    {
                        DevExpress.XtraEditors.Controls.EditorButton delete = new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph, imageCollection1.Images[4], null);
                        delete.Tag = "delete," + fn;
                        bnt.Properties.Buttons.Insert(0, delete);
                    }
                    b.Tag = ClassShare.JobAsign + @"\\" + fn;                   
                    bnt.Properties.Buttons.Insert(0, b);
                }
                GC.Collect();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private void bnt_FileAttach_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            try
            {
                if (e.Button.Tag.ToString() == "Add")
                {
                    OpenFileDialog file = new OpenFileDialog();
                    file.Multiselect = true;
                    if (file.ShowDialog() == DialogResult.OK)
                    {
                        for (int i = 0; i < file.FileNames.Length; i++)
                        {
                            if (!Directory.Exists(ClassShare.JobAsign))
                                Directory.CreateDirectory(ClassShare.JobAsign);
                            string ex = Path.GetExtension(file.FileNames[i]);
                            string name = Path.GetFileNameWithoutExtension(file.FileNames[i]) + DateTime.Now.ToString("ddMMyyhhmmss");
                            name = name + ex;
                            string _Path = Path.GetFullPath(file.FileNames[i]);
                            File.Copy(_Path, ClassShare.JobAsign + @"\\" + name, true);
                            ListAttachFile.Add(name);
                        }
                        DisplayAtachfiles(ListAttachFile, bnt_FileAttach);
                    }
                }
                else
                {
                    string[] tag = e.Button.Tag.ToString().Split(new string[] { "," },0);
                    if (tag[0] == "delete")
                    {
                        bnt_FileAttach.Properties.Buttons.RemoveAt(e.Button.Index);
                        bnt_FileAttach.Properties.Buttons.RemoveAt(e.Button.Index+1);
                        if (ListAttachFile.Count() > 0)
                        {
                            for(int i=0;i<ListAttachFile.Count();i++)
                            {
                                if (tag[1].ToString() == ListAttachFile[i].ToString())
                                    ListAttachFile.RemoveAt(i);
                            }
                        }
                    }
                    else
                        System.Diagnostics.Process.Start(e.Button.Tag.ToString());
                }
            }
            catch
            {
                MessageBox.Show("Not find File Attach!", "Not Find...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public List<string> GetValue()
        {
            ListValue.Add(txt_actionContent.EditValue.ToString());
            ListValue.Add(txt_actionRequestTime.EditValue.ToString());
            if (ListAttachFile.Count() > 0)
            {
                fn = ListAttachFile[0].ToString();
                for (int i = 1; i < ListAttachFile.Count(); i++)
                {
                    fn += "," + ListAttachFile[i].ToString();
                }
            }            
            ListValue.Add(fn);
            
            return ListValue;
        }

        private void txt_Status_EditValueChanged(object sender, EventArgs e)
        {
            if (txt_Status.Text == "Approve")
                txt_Status.ForeColor = Color.Green;
            if (txt_Status.Text == "Reject")
                txt_Status.ForeColor = Color.Red;
            if (txt_Status.Text == "Waiting For Confirm")
                txt_Status.ForeColor = Color.Blue; 
        }
    }    
}
